$(function(){
    var w = $(window).width()
    var h = $(window).height()
    if(w > h){
        $('body').addClass('landscape');
    }
})

/**************************************时间格式化处理************************************/
function dateFtt(fmt,date){ 
  var o = {   
    "M+" : date.getMonth()+1,                 //月份   
    "d+" : date.getDate(),                    //日   
    "h+" : date.getHours(),                   //小时   
    "m+" : date.getMinutes(),                 //分   
    "s+" : date.getSeconds(),                 //秒   
    "q+" : Math.floor((date.getMonth()+3)/3), //季度   
    "S"  : date.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}

Vue.filter('formatDateTime', function(time){
        var d = new Date(time);
    return dateFtt("hh:mm:ss",d);
});

window.vm = new Vue({
        el: '#app',
        data: {
                mode: 'loading', //模式loading, factory, error
                logs: [
                        {
                                time: new Date().getTime(),
                                text: '准备初始化...'
                        }
                ], 
                machineSn: '',
                errorText: '无法正常加载...'
        },
        methods: {
                log: function(msg){
                        this.logs.push({
                                time: new Date().getTime(),
                                text: msg
                        });
                },
                gotoActive: function(){
                    var that = this;
                    $('#app').hide();
                    $('#J-machine-sn').text("设备编码："+this.machineSn);
                    $('#active-wrap').removeClass('hide');
                    xphone.activeUrl(function(r){
                        $('#active-qr').html('');
                        $('#active-qr').qrcode({
                            render: 'canvas',
                            text: r.data.url
                        });

                        //检测
                        var checkTimer;
                        var checkFn = function(){
                            checkTimer = setTimeout(function(){
                                that.initToken(function(){
                                    clearTimeout(checkTimer);
                                });

                                checkFn();
                            }, 5 * 1000);
                        }

                        checkFn();

                    }, function(r){
                    });

                },
                initNetwork: function(){
                        var that = this;
                        that.log('检查网络...');
                        xphone.testNetwork(function(){
                                that.log('网络正常！');
                                //初始化密钥系统
                                that.initMachineKey();
                        }, function(result){
                                that.log(result.message);
                                setTimeout(that.initNetwork, 10 * 1000);
                        });


                },
                initMachineKey: function(){
                        var that = this;

                         xphone.getMachineInstallKey(function(result){
                                 that.log('检测到安装密钥：'+result.data.key);
                                 that.log('申请机器密钥...');
                                 xphone.acquireMachineSecret(function(result){
                                    that.log('已下载机器密钥：'+result.data.secret);

                                    //TODO

                                    xphone.activeStatus(function(r){
                                        console.log('activeStatus: '+ r);
                                        if(r.data.activeStatus){
                                            //有密钥
                                            that.initToken();
                                        }else{
                                            that.gotoActive();
                                        }

                                    }, function(r){
                                        that.log('无法获取激活状态，请联系厂商');
                                    })


                                 }, function(result){
                                         that.log(result.message);
                                 });
                         }, function(result){
                                 that.log(result.message);
                                 xphone.getSetting('secret', function(result){


                                         xphone.activeStatus(function(r){
                                             console.log('activeStatus: '+ r);
                                             if(r.data.activeStatus){
                                                 //有密钥
                                                 that.initToken();
                                             }else{
                                                that.gotoActive();
                                             }

                                          }, function(r){
                                            that.log('无法获取激活状态，请联系厂商');
                                          })

                                  }, function(result){
                                         //无密钥
                                         that.log(result.message);
                                  })
                         });

                },
                initToken: function(callback){

                        var that = this;
                        callback = callback || function(){};

                        that.log('获取AccessToken...');
                        xphone.acquireAccessToken(function(result){
                                that.log('已下载AccessToken：'+result.data.token);
                                callback();

                               xphone.replaceView({
                                   path: '/main'
                               },function(result){

                               }, function(result){
                                   xphone.toast(result.message);
                               });
                        }, function(result){
                                that.log(result.message);
                        });
                },
                saveMachineSn: function(){
                        var that = this;
                        xphone.toast(this.machineSn);
                        xphone.saveSetting('machineSn', this.machineSn, function(){
                                that.init();
                        });
                },
                gotoFactory: function(){
                        this.mode = 'factory';
                },
                init: function(){
                        var that = this;
                        this.mode = 'loading';
                        //开始
                        xphone.getSetting('machineSn', function(result){
                                //有sn
                                that.machineSn = result.data;
                                that.log('机器编码：'+that.machineSn);
                                that.initNetwork();
                        }, function(){
                                //无sn
                                that.mode = 'factory';
                        })
                },
                testVideo: function(){
                    var that = this;
                    xphone.playVideo('http://gmall-machine.test.gzhaoku.com/static/output4.mp4',function(result){
                           //播放结束
                           that.log('播放结束');
                   }, function(result){
                           that.log(result.message);
                   });
                }
        },
        mounted: function(){
                this.init();
        }
});